<template>
    <orh-trees :treesHead="treesHead" add="add"  @on-add="addShow" del="del" @on-del="confirm">
        <current-term @on-formstate="formState" style="width:200px" slot="option"></current-term>
        <Select v-model="curclass" slot="option" @on-change="eclassChange" size="small" style="width: 200px;margin-left:15px;" placeholder="请选择班级">
            <Option v-for="(item,index) in eclassList" :value="item.eclass_id" :key="index">{{ item.eclass_name.zh?item.eclass_name.zh:item.eclass_name.mn }}</Option>
        </Select>
        <Tree :data="trees" @on-select-change="getNodes"  slot="trees"></Tree>
        <orh-sub-table pages :pageDate="pageDate" @on-page-change="queryData"  slot="content">
            <Table :data="DorLeaveList" :columns="dataKey" @on-selection-change="selectOne" slot="table" :loading="loading"></Table>
        </orh-sub-table>
        <orh-sidebar v-model="show" :title="chaxunArr.name" scale slot="others" >
            <component :is="content" :chaxunArr="chaxunArr" :trees="trees" :infoId="infoId" @reload="load" @requery='requery'></component>
        </orh-sidebar>
    </orh-trees>
</template>

<script>
  export default {
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
        ClassLeaveOperation: resolve => {require(['../ClassLeave/Operation.vue'], resolve);},
        LodgingLeaveOperation: resolve => {require(['../LodgingLeave/Operation.vue'], resolve);},
        CurrentTerm: resolve=> require(['&/Authpub/CurrentTerm.vue'],resolve)   
    },
    name: 'leave-studentleave',
    data() {
        return {
            allid:null,
            treeId:null,
            curclass:'',
            eclassList:[],
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 1
            },
            treesHead: {
                title: '请假类型',    //#树形菜单标题
                button: false,    //#是否显示按钮
                button_text: '添加按钮'    //#按钮文字
            },
            content:null,
            del:[],
            delUrl:null,
            show:false,
            url:null,
            dataKey:[],
            infoId: null,
            loading:false,
            chaxunArr:{
                id:null,
                name:null,
                type:null,
            },
            right:null,
            trees: [], 
            DorLeaveList:[],
            //外宿请假
            dormcolumns: [
            {
                type: 'selection',
                align: 'center',
                width: 60,
                fixed: 'left', 
            },
            {
                title: '学生姓名',
                minWidth: 200,
                align: 'center',
                fixed: 'left', 
                render: (h, params) => {
                    if(params.row.student!=null){
                        return h('div', `${params.row.student.student_name.zh}${params.row.student.identityard_num}`);
                    }
                }
            },
            {
                title: '宿舍房间号',
                minWidth: 120,
                align: 'center',
                render: (h, params) => {
                    return h('div', params.row.bed_relation!=null?params.row.bed_relation.dormitory.rooms.name.zh:'');
                }
            },
            {
                title: '床位号',
                minWidth: 100, 
                align: 'center',
                render: (h, params) => {
                    return h('div', params.row.bed_relation!=null?params.row.bed_relation.bed.bed_code:'');
                }
            },
            {
                title: '班级',
                minWidth: 100,
                align: 'center',
                render: (h, params) => {
                    return h('div', params.row.eclass!=null?params.row.eclass.eclass_name.zh:'');
                }
            },
            {
                title: '请假开始时间',
                minWidth: 200,
                align: 'center',
                key: 'start_time'
            },
            {
                title: '请假结束时间',
                minWidth: 200,
                align: 'center',
                key: 'end_time'
            },
            {
                title: '请假分类',
                minWidth: 100,
                align: 'center',
                render: (h, params) => {
                    if(params.row.leave!=null){
                        return h('div', params.row.leave.leave_classifications.dictionary_name.zh);
                    }
                }
            },
            {
                title: '班主任是否担保',
                minWidth: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('div', params.row.is_guarantee=='0'?'不担保':'担保')
                    ]);
                }
            },
            {
                title: '请假来源',
                minWidth: 100,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('div',params.row.type=='0'?'外出':'外宿')
                    ]);
                }
            },
           {
            title: '操作',
            key: 'action',
            align: 'center',
            width: 150,   
            fixed: 'right',           
            render: (h, params) => {
                let ss = params.row.type
                if(ss == '1'){
                return h('div', [
                    h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px',
                        },
                        on: {
                            click: () => {
                                this.update(params.index)
                            }
                        }
                    }, '编辑'),
                    h('Button', {
						directives: [
							{
								name: "button",
								value: "del"
							}
						],
						props: {
							type: 'error',
							size: 'small'
						},
						style: {
							marginRight: '5px',
						},
						on: {
							click: () => {
								this.del = [params.row.dormitory_outside_id]
								this.confirm()
							}
						}
					}, '删除'),
                    
                ]);
                }
            }
        }
        ],
        //  进宿请假
        lodgingcolumns: [
            {
                type: 'selection',
                align: 'center',
                width: 60,
                fixed: 'left', 
            },
            {
                title: '学生姓名',
                align: 'center',
                fixed: 'left', 
                width: 200,
                render: (h, params) => {
                    if(params.row.student!=null){
                        return h('div', params.row.student.student_name.zh);
                    }
                }
            },
            {
                title: '宿舍楼',
                align: 'center',
                minWidth: 200,
                render: (h, params) => {
                    return h('div', params.row.buildings!=null?params.row.buildings.buildings_name.zh:'');
                }
            },
            {
                title: '房间',
                align: 'center',
                minWidth: 150,
                render: (h, params) => {
                    return h('div', params.row.room!=null?params.row.room.name.zh:'');
                }
            },
            {
                title: '班级',
                align: 'center',
                minWidth: 100,
                render: (h, params) => {
                    return h('div', params.row.eclass!=null?params.row.eclass.eclass_name.zh:'');
                }
            },
            {
                title: '时间',
                minWidth: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', params.row.created_at);
                }
            },
            {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                fixed: 'right', 
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.update(params.index)
                                }
                            }
                        }, '编辑'),
                        h('Button', {
                            directives: [
                                {
                                    name: "button",
                                    value: "del"
                                }
                            ],
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.del = [params.row.id]
                                    this.confirm()
                                }
                            }
                        }, '删除'),
                    ]);
                }
            }
        ],
      //  外出请假
        classcolumns: [
            {
                type: 'selection',
                align: 'center',
                width: 60,
                fixed: 'left', 
            },
            {
                title: '学生姓名',
                align: 'center',
                fixed: 'left', 
                width: 200,
                render: (h, params) => {
                if(params.row.student!=null){
                    let ss = `${params.row.student.student_name.zh}${params.row.student.identityard_num}`
                    return h('div', ss);
                }
                }
            },
            {
                title: '班级',
                align: 'center',
                minWidth: 100,
                render: (h, params) => {
                    return h('div',params.row.eclass!=null?params.row.eclass.eclass_name.zh:'');
                }
            },
            {
                title: '请假开始时间',
                align: 'center',
                minWidth: 200,
                key: 'start_time'
            },
            {
                title: '请假结束时间',
                align: 'center',
                minWidth: 200,
                key: 'end_time'
            },
            {
                title: '请假分类',
                align: 'center',
                minWidth: 150,
                render: (h, params) => {
                    return h('div', params.row.leave!=null?params.row.leave.leave_classifications.dictionary_name.zh:'');
                }
            },  
            {
                title: '操作',
                key: 'action',
                align: 'center',
                width: 200,
                fixed: 'right',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.update(params.index)
                                }
                            }
                        }, '编辑'),
                        h('Button', {
                            directives: [
                                {
                                    name: "button",
                                    value: "del"
                                }
                            ],
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.del = [params.row.eclass_leave_id]
                                    this.confirm()
                                }
                            }
                        }, '删除'),
                    ]);
                }
            }
        ],
      }
    },
    methods: {
    /************************************************************
     * 获取班级
     ************************************************************/
    classData() {
        this.$http.get('api/safety/v1/certifications/classlist').then(val=>{
            this.eclassList = val.data
            if(val.data.length!=0){
                this.curclass = this.eclassList[0].eclass_id
                this.eclassChange(this.curclass)
            }
        }).catch(err => {
            if (err.code && err.code == 422) {
                this.$Message.error(err.message);
            }
        })
    },
    /************************************************************
     * 选择班级
     ************************************************************/
    eclassChange(id){
        if(id!=''){
            this.curclass = id
            this.LeaveState(this.treeId)
        }
    },
    /************************************************************
     * 获取请假类型
     ************************************************************/
    load() {
        let defaul=[]
        this.$http.get('api/dictionary/v1/dictionary/pidquerys?student_leave_kind').then(res=>{
            this.trees=this.process(res.data.student_leave_kind)
            defaul.push(this.trees[0])                      
            this.getNodes(defaul)
        }).catch(err => {
            if (err.code && err.code == 422) {
                this.$Message.error(err.message);
            }
        })
    },
    /************************************************************
     * 处理数据
     ************************************************************/
    process(data) {
        let arr = []
        data.map(item => {
            item.title = item.dictionary_name.zh;
            item.id = item.dictionary_id;
            item.type = item.value_code
            arr.push(item)
        });
        return arr
    },
    /************************************************************
     * 点击树
     ************************************************************/
    getNodes(arr) {      
        if(arr.length!=0){            
            this.right=arr[0].type //默认    
            this.chaxunArr.id=arr[0].dictionary_id;
            this.chaxunArr.name=arr[0].title;
            this.chaxunArr.type=arr[0].type;
            this.LeaveState(arr[0].type);
        }
    },
    /************************************************************
     * 判断请求数据接口
     ************************************************************/
    LeaveState(val){       
        if(val!=null){
            this.treeId = val
            if(this.curclass!=''){
                if(this.allid!=null){
                    //外宿请假
                    if(val == 'dormitory_leave'){
                        this.url =`api/leave/v1/student_leave/myclass_dormitory_leave_index?eclass_id=${this.curclass}&semester_id=${this.allid}&page=`
                        this.dataKey = this.dormcolumns
                    //进宿请假
                    }else if(val == 'lodging_leave'){
                        this.url = `api/leave/v1/leave_dormitory/my_student_index?eclass_id=${this.curclass}&semester_id=${this.allid}&page=`
                        this.dataKey = this.lodgingcolumns                       
                    //外出请假
                    }else{
                        this.url = `api/leave/v1/student_leave/myclass_eclass_leave_index?eclass_id=${this.curclass}&semester_id=${this.allid}&page=`
                        this.dataKey = this.classcolumns
                    }
                    this.queryData(1)
                }
            }
        }
    },
    /************************************************************
     * 获取数据
     ************************************************************/
    queryData(index){
        this.DorLeaveList = []
        this.loading=true
        this.$http.get(`${this.url}${index}`).then(val=>{
            this.DorLeaveList = val.data            
            if(val.pages!=null){
                this.pageDate.total=val.pages.total
                this.pageDate.per_page=val.pages.per_page
                this.pageDate.current_page=val.pages.current_page
            }
            this.loading=false
        }).catch(err => {
            if (err.code && err.code == 422) {
                this.$Message.error(err.message);
            }
            this.loading = false
        })
    },
    /************************************************************
     * 添加
     ************************************************************/
    addShow() {
        this.show = true
        this.right = this.chaxunArr.type   
        this.infoId = null                    
        if(this.right =='dormitory_leave'){
            this.content = 'Operation'  
        }else if(this.right =='lodging_leave'){
            this.content = 'LodgingLeaveOperation'
        }else if(this.right =='class_leave'){
            this.content = 'ClassLeaveOperation'
        }
    },
    /************************************************************
     * 批量删除
     ************************************************************/
    selectOne(index){
        this.del = []
        if(this.right =='dormitory_leave'){
            this.del = index.map(item=>item.dormitory_outside_id)        
        }else if(this.right == 'class_leave'){ 
            this.del = index.map(item=>item.eclass_leave_id) 
        }else{
            this.del = index.map(item=>item.id) 
        }
    },
    /************************************************************
     * 执行删除操作
     ************************************************************/
    confirm () {
        this.$Modal.confirm({
            title: '数据删除',
            content: '<p>您确定要删除这些数据吗?</p>',
            onOk: () => {
                if(this.del.length==0){
                    this.$Message.error('请选择删除项');
                    return
                }
                this.del = JSON.stringify(this.del)
                this.tableload = true 
                if(this.right =='dormitory_leave'){
                    this.delUrl = `api/leave/v1/student_leave/dormitory_leave_delete/${this.del}`
                }else if(this.right =='class_leave'){
                    this.delUrl = `api/leave/v1/student_leave/eclass_leave_delete/${this.del}`
                }else{
                    this.delUrl = `api/leave/v1/leave_dormitory/delete/${this.del}`
                }
                this.deletes()
            },
        });
    },
    /************************************************************
     * 删除
     ************************************************************/
    deletes(){
        this.$http.delete(this.delUrl).then(val=>{  
            this.del=[]
            this.queryData(this.pageDate.current_page)    
            this.$Message.success(val.message);           
        }).catch(err=>{
            if(err.code && err.code==422) {
                this.$Message.error(err.message);
            }
            this.del=[]
            this.tableload = false 
        })
    },
    /************************************************************
     * 修改
     ************************************************************/
    update(index){
        this.show = true   
        if(this.right =='dormitory_leave'){
            this.content = 'Operation'  
            this.infoId = `${this.DorLeaveList[index].dormitory_outside_id}`       
        }else if(this.right =='class_leave'){
            this.content = 'ClassLeaveOperation'
            this.infoId = `${this.DorLeaveList[index].eclass_leave_id}`
        }else if(this.right =='lodging_leave'){
            this.content = 'LodgingLeaveOperation'
            this.infoId = `${this.DorLeaveList[index].id}`
        }else{
            this.infoId = null
        }
    },
    /************************************************************
     * 数据更新
     ************************************************************/
    requery(val){
        if(val == true){
            this.show=false
            this.queryData(this.pageDate.current_page);
        }
    },
    /************************************************************
     * 选择学期
     ************************************************************/
    formState(val){  
        if(val!=undefined || val!=null){    
            this.allid = val
            this.eclassChange(this.curclass)
        }else{
            this.loading=false
        }
    },  
    },
    created() {
        this.load();
        this.classData();
    }
}
</script>




